package org.jeecg.modules.demo.yxpingdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.utils.HttpClientUtil;
import org.jeecg.modules.demo.yxpingdata.entity.YxPinpData;
import org.jeecg.modules.demo.yxpingdata.service.IYxPinpDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: yx_pinp_data
 * @Author: jeecg-boot
 * @Date:   2022-11-30
 * @Version: V1.0
 */
@Api(tags="yx_pinp_data")
@RestController
@RequestMapping("/yxpingdata/yxPinpData")
@Slf4j
public class YxPinpDataController extends JeecgController<YxPinpData, IYxPinpDataService> {
	@Autowired
	private IYxPinpDataService yxPinpDataService;
	@Autowired
	private ISysBaseAPI sysBaseAPI;

	 @Value(value = "${reloadPingDataUrl}")
	 private String reloadPingDataUrl;


	/**
	 * 分页列表查询
	 *
	 * @param yxPinpData
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "煤种管理-分页列表查询")
	@ApiOperation(value="煤种管理-分页列表查询", notes="煤种管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(YxPinpData yxPinpData,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxPinpData> queryWrapper = QueryGenerator.initQueryWrapper(yxPinpData, req.getParameterMap());
		Page<YxPinpData> page = new Page<YxPinpData>(pageNo, pageSize);
		IPage<YxPinpData> pageList = yxPinpDataService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	 /**
	  *  同步
	  *
	  * @return
	  */
	 @AutoLog(value = "煤种管理-同步")
	 @ApiOperation(value="煤种管理-同步", notes="煤种管理-同步")
	 @PostMapping(value = "/reloadPingData")
	 public Result<?> reloadPingData(HttpServletRequest req) {
		 try{
			 // 获取登录用户信息
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 List<String> strings = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
			 if(strings.contains("admin")||strings.contains("administrator")){
				 String json = HttpClientUtil.doGet(reloadPingDataUrl);
				 JSONObject jsonObject = (JSONObject) JSONObject.parse(json);
				 String JsonStr  = jsonObject.get("data").toString();
				 List<YxPinpData> pingDataList = JSONArray.parseArray(JsonStr,YxPinpData.class);
				 List<String> ids  = yxPinpDataService.getList();
				 if(!ids.isEmpty()){
					 yxPinpDataService.removeByIds(ids);
				 }
				 for (YxPinpData yxPinpData : pingDataList) {
					 yxPinpData.setId(null);
					 yxPinpData.setCreateTime(new Date());
					 yxPinpData.setStatus(0);
					 yxPinpDataService.save(yxPinpData);

				 }
				 return Result.OK("同步数据成功!");
			 }else{
				 return  Result.error(500,"您无此权限，请联系管理员！");
			 }


		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }
	 }
	@PostMapping(value = "/qyPingData")
	public Result<?> qyPingData(@RequestBody YxPinpData yxPinpData){
	 	try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			List<String> strings = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
			if(strings.contains("admin")||strings.contains("administrator")){
				if(yxPinpData.getStatus()==0){
					return Result.error(500,"该数据已是启用状态，无需重复操作！");
				}
				yxPinpData.setStatus(0);
				yxPinpDataService.updateById(yxPinpData);
				return Result.OK("启用成功！");
			}else{
				return  Result.error(500,"您无此权限，请联系管理员！");
			}
		}catch (Exception e ){
	 		e.printStackTrace();
	 		return Result.error(500,"系统错误！");
		}
	}
	@PostMapping(value = "/jyPingData")
	public Result<?> jyPingData(@RequestBody YxPinpData yxPinpData){
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			List<String> strings = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
			if(strings.contains("admin")||strings.contains("administrator")){
				if(yxPinpData.getStatus()==1){
					return Result.error(500,"该数据已是禁用状态，无需重复操作！");
				}
				yxPinpData.setStatus(1);
				yxPinpDataService.updateById(yxPinpData);
				return Result.OK("禁用成功！");
			}else{
				return  Result.error(500,"您无此权限，请联系管理员！");
			}

		}catch (Exception e ){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	@PostMapping(value = "/getPingByStr")
	 public String getPingByStr(@RequestParam(name="str",required=true)String str){
		 Gson gson = new Gson();
		 Map map = new HashMap();
	 	try{
			QueryWrapper<YxPinpData> queryWrapper = new QueryWrapper<>();
			queryWrapper.like(StringUtils.isNotBlank(str),"VARIETYNAME", str);
			queryWrapper.eq("STATUS",0);
	 		List<YxPinpData> mapList = yxPinpDataService.list(queryWrapper);
			map.put("code",200);
			map.put("message","查询成功！");
	 		map.put("data",mapList);
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
	 		e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	 }


	/**
	 *   添加
	 *
	 * @param yxPinpData
	 * @return
	 */
	@AutoLog(value = "煤种管理-添加")
	@ApiOperation(value="煤种管理-添加", notes="煤种管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxPinpData yxPinpData) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			List<String> strings = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
			if(strings.contains("admin")||strings.contains("administrator")){
				yxPinpData.setCreateTime(new Date());
				yxPinpDataService.save(yxPinpData);
				return Result.OK("添加成功！");
			}else{
				return  Result.error(500,"您无此权限，请联系管理员！");
			}
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param yxPinpData
	 * @return
	 */
	@AutoLog(value = "煤种管理-编辑")
	@ApiOperation(value="煤种管理-编辑", notes="煤种管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxPinpData yxPinpData) {
		yxPinpDataService.updateById(yxPinpData);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "煤种管理-通过id删除")
	@ApiOperation(value="煤种管理-通过id删除", notes="煤种管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		yxPinpDataService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "煤种管理-批量删除")
	@ApiOperation(value="煤种管理-批量删除", notes="煤种管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxPinpDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "煤种管理-通过id查询")
	@ApiOperation(value="煤种管理-通过id查询", notes="煤种管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxPinpData yxPinpData = yxPinpDataService.getById(id);
		if(yxPinpData==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxPinpData);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxPinpData
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxPinpData yxPinpData) {
        return super.exportXls(request, yxPinpData, YxPinpData.class, "yx_pinp_data");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxPinpData.class);
    }

}
